<?php
define("THIS_PAGE", "clients");
require_once($_SERVER["DOCUMENT_ROOT"] . "/../sys/libs/init.lib");
include($_SERVER["DOCUMENT_ROOT"] . "/../sys/includes/adminheader.php");

if (empty($_REQUEST["do"])) {
	header("Location: ?do=index");
}

$do = strtolower(trim($_REQUEST["do"]));

print "<div class=\"pagebody\">";

if ($do == "index") {


} else if ($do == "new") {

print "<h1>Add New Client</h1>";

	if (!isset($_REQUEST["submit"]) || strtolower($_REQUEST["submit"]) != "add") {

		$form = new formBuilder("NewClient", array("action" => $_SERVER["PHP_SELF"], "method" => "post"));

		print "	<table width\"600\">
			<tr>
				<td>Business Name</td>
				<td>" . $form->hidden(array("name" => "do", "value" => "new")) . $form->input(array("name" => "frm_businessname", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>ABN</td>
				<td>" . $form->input(array("name" => "frm_abn", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>ACN</td>
				<td>" . $form->input(array("name" => "frm_acn", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>Address</td>
				<td>" . $form->input(array("name" => "frm_address", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>Address 2</td>
				<td>" . $form->input(array("name" => "frm_address2", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>Post/Zip Code</td>
				<td>" . $form->input(array("name" => "frm_postcode", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>City</td>
				<td>" . $form->input(array("name" => "frm_city", "class" => "forminput")) . "</td>
			</tr>
			<tr>
				<td>Country</td>
				<td>" . country_options() . "</td>
			</tr>
			<tr>
				<td></td>
				<td>" . $form->submit(array("name" => "submit", "value" => "Add")) . "</td>
			</tr>
			</table></form>";
	} else if (strtolower(trim($_REQUEST["submit"])) == "add") {

		$savedata = array();
		foreach ($_REQUEST as $key => $val) {
			if (substr($key, 0, 4) == "frm_") {
				$savedata[substr($key, 4)] = $db->quote($val);
			}
		}

		if ($cid = $db->insert("clients", $savedata)) {
			print "Record inserted successfully. <br />";
			print "Would you like to add this client to a user account? <br />";
			
			print "<form method=\"post\" action=\"admin_users\">
					<table width=\"400\" border=\"0\">
					<tr>
						<td><input type=\"hidden\" name=\"clientid\" id=\"clientid\" value=\"{$cid}\" />Select User Account:</td>
						<td>
						<select name=\"\">
						<option value=\"\">Choose</option>";
						
						$userSQL = "SELECT id, CONCAT(FirstName, ' ', Surname, ' / ', Username) AS Name FROM users";
						while ($u = $db->getnext($userSQL)) {
							print "<option value=\"{$u->id}\">{$u->Name}</option>";
						}
						
			print "		</select>
						</td>
					</tr>
					</table>
					</form>";
		}
	}
} else if ($do == "list") {

	if (isset($_REQUEST["editclient"]) && !empty($_REQUEST["editclient"])){
		$querystring = query_string_toarray($_SERVER["QUERY_STRING"]);
		unset($querystring["do"]);
		$querystring["do"] = "edit";
		$querystring = query_array_tostring($querystring);
		header("Location: ?".$querystring);
	}

	if (isset($_REQUEST["deleteclient"]) && !empty($_REQUEST["deleteclient"])){
		$querystring = query_string_toarray($_SERVER["QUERY_STRING"]);
		unset($querystring["do"]);
		$querystring["do"] = "delete";
		$querystring = query_array_tostring($querystring);
		header("Location: ?".$querystring);
	}

	$SQL = "SELECT c.*, u.UserName \n"
		 . "  FROM clients c \n"
		 . "       LEFT JOIN client_user cu ON cu.ClientID = c.id \n"
		 . "       LEFT JOIN users u ON u.id = cu.UserID  \n";

	$clients = $db->getarray($SQL);
	$form = new formBuilder;
	print $form->hidden(array("name" => "do", "value" => "list"));
	print "<table width=\"100%\" border=\"0\" cellspacing=\"0\">
		   <tr>
				<th>Business Name</th>
				<th>User Name</th>
				<th>Last Invoiced</th>
				<th>Amount Outstanding</th>
				<th>E X</th>
		   </tr>";
	
	$row1 = "#ffffff";
	$row2 = "#9999ff";

	for ($i=0; $i<count($clients); $i++) {
		$bgcolor = ($i % 2) ? $row1 : $row2;
		print "<tr style=\"background-color:{$bgcolor};\"><td>{$clients[$i]->BusinessName}</td>
				<td>{$clients[$i]->UserName}</td>
				<td></td><td></td><td align=\"center\">" . $form->checkbox(array("name" => "editclient", "value" => $clients[$i]->id, "title" => "Edit " . $clients[$i]->BusinessName, "alt" => "Edit " . $clients[$i]->BusinessName)) . " " . $form->checkbox(array("name" => "deleteclient", "value" => $clients[$i]->id, "title" => "Delete " . $clients[$i]->BusinessName, "alt" => "Delete " . $clients[$i]->BusinessName)) . "</td></tr>";
	}

	print "<tr>
		   		<td colspan=\"4\"></td>
				<td>". $form->submit() ."</td>
		   </tr></table>";

} else if ($do == "edit") {

	print "<h1>Edit Client</h1>";

	$SQL = "SELECT c.*, u.UserName, u.id AS UserID \n"
		  ."  FROM clients c \n"
		  ."       LEFT JOIN client_user cu ON cu.ClientID = c.id \n"
		  ."       LEFT JOIN users u ON u.id = cu.UserID \n"
		  ." WHERE c.id = {$_REQUEST["editclient"]}\n";

	$client = $db->get($SQL);

	if (!isset($_REQUEST["submit"]) || strtolower($_REQUEST["submit"]) != "edit") {

		$form = new formBuilder("EditClient", array("action" => $_SERVER["PHP_SELF"], "method" => "post"));

		print "	<table width\"600\">
			<tr>
				<td>Business Name</td>
				<td>" . $form->hidden(array("name" => "do", "value" => "edit")) . $form->hidden(array("name" => "clientid", "value" => "{$client->id}")) . $form->input(array("name" => "frm_businessname", "class" => "forminput", "value" => $client->BusinessName)) . "</td>
			</tr>
			<tr>
				<td>ABN</td>
				<td>" . $form->input(array("name" => "frm_abn", "class" => "forminput", "value" => $client->ABN)) . "</td>
			</tr>
			<tr>
				<td>ACN</td>
				<td>" . $form->input(array("name" => "frm_acn", "class" => "forminput", "value" => $client->ACN)) . "</td>
			</tr>
			<tr>
				<td>Address</td>
				<td>" . $form->input(array("name" => "frm_address", "class" => "forminput", "value" => $client->Address)) . "</td>
			</tr>
			<tr>
				<td>Address 2</td>
				<td>" . $form->input(array("name" => "frm_address2", "class" => "forminput", "value" => $client->Address2)) . "</td>
			</tr>
			<tr>
				<td>Post/Zip Code</td>
				<td>" . $form->input(array("name" => "frm_postcode", "class" => "forminput", "value" => $client->PostCode)) . "</td>
			</tr>
			<tr>
				<td>City</td>
				<td>" . $form->input(array("name" => "frm_city", "class" => "forminput", "value" => $client->City)) . "</td>
			</tr>
			<tr>
				<td>Country</td>
				<td>" . country_options("frm_country", $client->Country) . "</td>
			</tr>
			<tr>
				<td></td>
				<td>" . $form->submit(array("name" => "submit", "value" => "Edit")) . "</td>
			</tr>
			</table></form>";
	} else if (strtolower(trim($_REQUEST["submit"])) == "edit") {

		$savedata = array();
		foreach ($_REQUEST as $key => $val) {
			if (substr($key, 0, 4) == "frm_") {
				$savedata[substr($key, 4)] = $val;
			}
		}

		if ($db->update("clients", $savedata, "id = {$_REQUEST["clientid"]}")) { // TODO - Finish this!!
			print "Record updated successfully.";
		}

	}

} else if ($do == "delete") {
	print "<h1>Delete Client</h1>";

	if (isset($_REQUEST["deleteclient"])) {
		$SQL = "SELECT c.*\n"
			."  FROM clients c \n"
			." WHERE c.id = {$_REQUEST["deleteclient"]}\n";
	
		$client = $db->get($SQL);
	
		$form = new formBuilder;
	
		print "<p>Are you sure you wish to delete \"{$client->BusinessName}\"?</p>";
		print $form->button(array("name" => "No", "value" => "No", "onclick" => "window.back(-1)"));
		print $form->button(array("name" => "Yes", "value" => "Yes", "onclick" => "submitDeleteForm({$client->id})"));
		print "</form>";
	
		print "<div style=\"display: none;\">";
		$hidden = new formBuilder("delform", array("action" => $_SERVER["PHP_SELF"], "method" => "post", "id" => "delform"));
		print $hidden->hidden(array("name" => "id", "id" => "delform_id"));
		print $hidden->hidden(array("name" => "do", "value" => "delete"));
		print "</form>";
		print "</div>";
	} else if (isset($_REQUEST["id"])) {

		if ($db->delete("clients", "id = {$_REQUEST["id"]}")) {
			print "Client deleted successfully";
		}

	}
	
}

print "</div>";
?>
